package com.nl.module_main.xiecheng

import androidx.test.core.app.ActivityScenario.launch
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking

/**
 * 作者: wangfei
 * 创建时期: 2022/12/1.
 * 包名: com.nl.module_main.xiecheng
 * 类名: NL_MedicaleEvaluation
 */
object Test02 {

    @JvmStatic
    fun main(args: Array<String>) {
        //test()
       // test01()
        test02()
    }

    fun test() = runBlocking {
            val chanel = Channel<Int>()
            launch {
                for (x in 1..5){
                    chanel.send(x * x)
                }
            }
            repeat(5){
                println("接受管道内容${chanel.receive()}")
            }
            println("Done!")
        }


    fun test01() = runBlocking {
        val chanel = Channel<Int>()
        launch {
            for (x in 1..5){
                chanel.send(x * x)
            }
        }
//        val iterator = chanel.iterator()
//        while (iterator.hasNext()){
//            val next = iterator.next()
//            println(next)
//        }
        for (x in chanel){
            println(x)
        }
        println("Done!")
    }

    fun test02() = runBlocking {
        val channel = Channel<Int>()
        launch {
            for (x in 1..5){
                channel.send(x * x)
            }
        }

        launch{
            delay(10)
            for (y in channel) {
                println(" 1 --> $y")
            }
        }

        launch {
            delay(20)
            for (y in channel) {
                println(" 2 --> $y")
            }
        }
        launch {
            delay(30)
            for (x in 90..100) channel.send(x)
            channel.close()
        }
    }




}